//
//  AppDelegate.swift
//  GitWeibo
//
//  Created by 胡金东 on 2017/5/17.
//  Copyright © 2017年 胡金东. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        /// -  设置额外的应用信息
        setUpAddtions()
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
        /// - 从服务器加载数据
        loadData()
        
        return true
    }

   

}
//MARK: -  设置额外的应用信息
extension AppDelegate{
    func setUpAddtions() {
        /// - 设置提示框时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        /// - 设置AFN网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        /// - ios10 以上的版本 (提示条/数值/声音/支付)
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound,.carPlay]){ (sucess,error) in
                print("授权" + (sucess ? "成功" : "失败"))
                
            }
        } else {
            /// - ios 10 以下版本 (提示条/数值/声音)
            let  notifySettings = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySettings)
        }
        

    }
}


//MARK: -  从服务器加载数据
extension AppDelegate{
    func loadData()  {
        /// - 异步加载
        DispatchQueue.global().async { 
            /// - 1.创建url
            let url = Bundle.main.url(forResource: "MainData.json", withExtension: nil)
            /// - 2.获取data
            let data = NSData(contentsOf: url!)
            /// - 3.取出文件，写入磁盘
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("MainData.json")
            /// - 直接保存在沙盒，等待下一次程序启动使用
            data?.write(toFile: jsonPath, atomically: true)
//            print("应用程序加载完毕\(jsonPath)")
        }
    }
}
